package top.haijunit.work.utils;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import lombok.extern.slf4j.Slf4j;

/**
 * 【说明类功能】
 *
 * @author zhanghaijun
 * @since 2024-08-22
 */
@Slf4j
public class DefaultExcelListener<T> extends AnalysisEventListener<T> {

    @Override
    public void invoke(T data, AnalysisContext analysisContext) {
        log.info("解析到一条数据:{}", data);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("sheet={} 所有数据解析完成！", context.readSheetHolder().getSheetName());
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        log.error("解析失败，但是继续解析下一行:{}", exception.getMessage());
        if (exception instanceof ExcelDataConvertException ex) {
            log.error("第{}行，第{}列解析异常，数据为:{}", ex.getRowIndex(), ex.getColumnIndex(), ex.getCellData());
        }
        super.onException(exception, context);
    }
}
